/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package paymentclient;

import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.LoginScreen;
import org.netbeans.microedition.lcdui.SplashScreen;
import org.netbeans.microedition.lcdui.WaitScreen;
import org.netbeans.microedition.util.SimpleCancellableTask;

/**
 * @author Og J. Ramos
 */
public class MobileLQ extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    private Command exitCommand;
    private Command okCommand;
    private Command helpCommand;
    private Command exitCommand1;
    private Form frmWelcome;
    private StringItem stringItem;
    private ImageItem imageItem;
    private StringItem stringItem1;
    private SplashScreen frmSplash;
    private LoginScreen frmLogin;
    private Alert alertSuccess;
    private WaitScreen waitScreen;
    private Alert alertFailure;
    private Image image1;
    private SimpleCancellableTask task;
//
    private boolean login = false;

    /**
     * The MobileLQ constructor.
     */
    public MobileLQ() {
    }

//
//

//
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is
    called before the
    startMIDlet method.
     */
    private void initialize() {
// write pre-initialize user code here

// write post-initialize user code here
    }
//

//
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started
    point.
     */
    public void startMIDlet() {
// write pre-action user code here
        switchDisplayable(null, getFrmSplash());
// write post-action user code here
    }
//

//
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed
    point.
     */
    public void resumeMIDlet() {
// write pre-action user code here

// write post-action user code here
    }
//

//
    /**
     * Switches a current displayable in a display. The display instance
    is taken from
    getDisplay method. This method is used by all actions in the design for
    switching
    displayable.
     * @param alert the Alert which is temporarily set to the display; if
    null, then
    nextDisplayable is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
// write post-switch user code here
    }
//

//
    /**
     * Called by a system to indicated that a command has been invoked on
    a particular
    displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {
// write pre-action user code here
        if (displayable == frmLogin) {
            if (command == LoginScreen.LOGIN_COMMAND) {
// write pre-action user code here
                switchDisplayable(null, getWaitScreen());
// write post-action user code here
            } else if (command == exitCommand1) {
// write pre-action user code here
                exitMIDlet();
// write post-action user code here
            }
        } else if (displayable == frmSplash) {
            if (command == SplashScreen.DISMISS_COMMAND) {
// write pre-action user code here
                switchDisplayable(null, getFrmLogin());
// write post-action user code here
            }
        } else if (displayable == frmWelcome) {
            if (command == exitCommand) {
// write pre-action user code here
                exitMIDlet();
// write post-action user code here
            } else if (command == helpCommand) {
// write pre-action user code here

// write post-action user code here
            } else if (command == okCommand) {
// write pre-action user code here

// write post-action user code here
            }
        } else if (displayable == waitScreen) {
            if (command == WaitScreen.FAILURE_COMMAND) {
// write pre-action user code here
                switchDisplayable(getAlertFailure(), getFrmLogin());
// write post-action user code here

            } else if (command == WaitScreen.SUCCESS_COMMAND) {
// write pre-action user code here
                switchDisplayable(getAlertSuccess(), getFrmWelcome());
// write post-action user code here
            }
        }
// write post-action user code here
    }
//

//
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {
// write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);
// write post-init user code here
        }
        return exitCommand;
    }
//

//
    /**
     * Returns an initiliazed instance of frmWelcome component.
     * @return the initialized component instance
     */
    public Form getFrmWelcome() {
        if (frmWelcome == null) {
// write pre-init user code here
            frmWelcome = new Form("Welcome", new Item[]{getImageItem(),
                        getStringItem(),
                        getStringItem1()
                    });
            frmWelcome.addCommand(getExitCommand());
            frmWelcome.addCommand(getOkCommand());
            frmWelcome.addCommand(getHelpCommand());
            frmWelcome.setCommandListener(this);
// write post-init user code here
        }
        return frmWelcome;
    }
//

//
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {
// write pre-init user code here
            stringItem = new StringItem("", "Welcome to the Airinet Link Quality Mobile Application", Item.PLAIN);
// write post-init user code here

        }
        return stringItem;
    }
//

//
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {
// write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);
// write post-init user code here
        }
        return okCommand;
    }
//

//
    /**
     * Returns an initiliazed instance of helpCommand component.
     * @return the initialized component instance
     */
    public Command getHelpCommand() {
        if (helpCommand == null) {
// write pre-init user code here
            helpCommand = new Command("Help", Command.HELP, 0);
// write post-init user code here
        }
        return helpCommand;
    }
//

//
    /**
     * Returns an initiliazed instance of exitCommand1 component.
     * @return the initialized component instance
     */
    public Command getExitCommand1() {
        if (exitCommand1 == null) {
// write pre-init user code here
            exitCommand1 = new Command("Exit", Command.EXIT, 0);
// write post-init user code here
        }
        return exitCommand1;
    }
//

//
    /**
     * Returns an initiliazed instance of imageItem component.
     * @return the initialized component instance
     */
    public ImageItem getImageItem() {
        if (imageItem == null) {
// write pre-init user code here
            imageItem = new ImageItem("", getImage1(),
                    ImageItem.LAYOUT_CENTER |
                    Item.LAYOUT_TOP | Item.LAYOUT_VCENTER | ImageItem.LAYOUT_NEWLINE_BEFORE |
                    ImageItem.LAYOUT_NEWLINE_AFTER, "");
// write post-init user code here
        }
        return imageItem;
    }
//

//
    /**
     * Returns an initiliazed instance of stringItem1 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem1() {
        if (stringItem1 == null) {
// write pre-init user code here
            stringItem1 = new StringItem("", "Select OK to Continue and HELP to read the help file.");
// write post-init user code here
}
return stringItem1;
}
//

//
/**
* Returns an initiliazed instance of frmSplash component.
* @return the initialized component instance
*/
public SplashScreen getFrmSplash() {
if (frmSplash == null) {
// write pre-init user code here
            frmSplash = new SplashScreen(getDisplay());
            frmSplash.setTitle("splashScreen");
            frmSplash.setCommandListener(this);
            frmSplash.setImage(getImage1());
            frmSplash.setText("Link Quality Mobile Application");
// write post-init user code here
        }
        return frmSplash;
    }
//

//
    /**
     * Returns an initiliazed instance of frmLogin component.
     * @return the initialized component instance
     */
    public LoginScreen getFrmLogin() {
        if (frmLogin == null) {
// write pre-init user code here
            frmLogin = new LoginScreen(getDisplay());
            frmLogin.setLabelTexts("Username", "Password");
            frmLogin.setTitle("loginScreen");
            frmLogin.addCommand(LoginScreen.LOGIN_COMMAND);
            frmLogin.addCommand(getExitCommand1());
            frmLogin.setCommandListener(this);
            frmLogin.setBGColor(-3355444);
            frmLogin.setFGColor(0);
            frmLogin.setUseLoginButton(false);
            frmLogin.setLoginButtonText("Login");
// write post-init user code here
        }
        return frmLogin;
    }
//

//
    /**
     * Returns an initiliazed instance of alertSuccess component.
     * @return the initialized component instance
     */
    public Alert getAlertSuccess() {
        if (alertSuccess == null) {
// write pre-init user code here
            alertSuccess = new Alert("Success");
            alertSuccess.setTimeout(Alert.FOREVER);
// write post-init user code here
        }
        return alertSuccess;
    }
//

//
    /**
     * Returns an initiliazed instance of waitScreen component.
     * @return the initialized component instance
     */
    public WaitScreen getWaitScreen() {
        if (waitScreen == null) {
// write pre-init user code here
            waitScreen = new WaitScreen(getDisplay());
            waitScreen.setTitle("waitScreen");
            waitScreen.setCommandListener(this);
            waitScreen.setTask(getTask());
// write post-init user code here
        }
        return waitScreen;
    }
//

//
    /**
     * Returns an initiliazed instance of image1 component.
     * @return the initialized component instance
     */
    public Image getImage1() {
        if (image1 == null) {
// write pre-init user code here
            try {
                image1 = Image.createImage("/logo.png");
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
// write post-init user code here
        }
        return image1;
    }
//

//
    /**
     * Returns an initiliazed instance of task component.
     * @return the initialized component instance
     */
    public SimpleCancellableTask getTask() {
        if (task == null) {
// write pre-init user code here
            task = new SimpleCancellableTask();
            task.setExecutable(new org.netbeans.microedition.util.Executable() {

                public void execute() throws Exception {
// write task-execution user code here
                    login();
                    if (login) {
                        getAlertSuccess().setString("Login Successfull.");
                    } else {
                        getAlertFailure().setString("Incorrect Username or Password");

                    }
                }
            });
// write post-init user code here
        }
        return task;
    }
//

//
    /**
     * Returns an initiliazed instance of alertFailure component.
     * @return the initialized component instance
     */
    public Alert getAlertFailure() {
        if (alertFailure == null) {
// write pre-init user code here
            alertFailure = new Alert("Failure");
            alertFailure.setTimeout(Alert.FOREVER);
// write post-init user code here
        }
        return alertFailure;
    }
//

    private void login() throws IOException {
        if (getFrmLogin().getUsername().equals("admin")) {
            if (getFrmLogin().getPassword().equals("!Air1n3t")) {
                login = true;
            } else {
                login = false;
            }
        } else {
            login = false;
        }
    }

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and
    initialize/starts or
    resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be
    unconditionally terminated
    and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
} 
